﻿using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using System.Collections;

public class MainGameController : MonoBehaviour {

	public HpBar hpBar;
	public InfoPanelController infoController;

	void Start() {

		if (SenceProp.backgroundColor == Color.white) {
			SenceProp.backgroundColor = (Color)new Color32 (74, 144, 226, 255);
		}

		// ==== define
		// ====================================
		GameObject frameBg;

		GameObject avatarPhoto;
		GameObject ap_photo;
		GameObject ap_cornar;
		GameObject ap_frame;
		GameObject ap_jobType;

		GameObject playerNamePanel;
		GameObject playerHpPanel;

		GameObject skillPanel;
		GameObject skillFrameLeft;
		GameObject skillFrameRight;
		GameObject skillContentLeft;
		GameObject skillContentRight;

		GameObject timePanel;

		// ==== find 
		// ====================================
		frameBg = GameObject.Find ("FrameBg");
		avatarPhoto = GameObject.Find ("AvatarPhoto");

		ap_photo = avatarPhoto.transform.FindChild ("PhotoMask").gameObject;
		ap_frame = avatarPhoto.transform.FindChild ("Frame").gameObject;
		ap_jobType = avatarPhoto.transform.FindChild ("JobType").gameObject;
		ap_cornar = avatarPhoto.transform.FindChild ("Cornar").gameObject;

		playerNamePanel = GameObject.Find ("PlayerNamePanel");
		playerHpPanel = GameObject.Find ("PlayerHpPanel");

		skillPanel = GameObject.Find ("SkillPanel");
		skillFrameLeft = UIUtility.FindChild (skillPanel,"Left/Frame");
		skillContentLeft = UIUtility.FindChild (skillPanel,"Left/Content");
		skillFrameRight = UIUtility.FindChild (skillPanel,"Right/Frame");
		skillContentRight = UIUtility.FindChild (skillPanel,"Right/Content");

		timePanel = GameObject.Find ("TimePanel");

		// ==== init 
		// ====================================


		GetComponent<Camera> ().backgroundColor = SenceProp.backgroundColor;
		hpBar.Init ();

		//frameBg.GetComponent<CanvasGroup> ().alpha = 0;
		frameBg.GetComponent<RawImage> ().color = SenceProp.backgroundColor;
		frameBg.GetComponent<RectTransform> ().localScale = new Vector3 (2, 2, 2);

		UIUtility.Alpha (ap_photo, 0);
		UIUtility.Alpha (ap_cornar, 0);
		UIUtility.Alpha (ap_frame, 0);
		UIUtility.Alpha (ap_jobType, 0);

		UIUtility.SetAnchoredPosition (ap_jobType, -50, -50);
		UIUtility.SetAnchoredPosition (ap_cornar, 50, 50);
		ap_frame.GetComponent<RectTransform> ().localScale = new Vector3 (.5f, .5f, .5f);

		UIUtility.SetAnchoredPositionX (playerNamePanel, 120);
		UIUtility.Alpha (playerNamePanel, 0);
		UIUtility.SetAnchoredPositionX (playerHpPanel, 100);
		UIUtility.Alpha (playerHpPanel, 0);

		UIUtility.FindChild (playerNamePanel, "Name").GetComponent<Text> ().text = SenceProp.PlayerName;

		UIUtility.Alpha (skillFrameLeft, 0);
		UIUtility.SetLocalScale (skillFrameLeft, 1.2f, 1.2f);
		UIUtility.Alpha (skillContentLeft, 0);
		UIUtility.Alpha (skillFrameRight, 0);
		UIUtility.SetLocalScale (skillFrameRight, 1.2f, 1.2f);
		UIUtility.Alpha (skillContentRight, 0);

		UIUtility.Alpha (timePanel, 0);
		UIUtility.SetAnchoredPositionY (timePanel, 0);

		infoController.Close ();

		// ==== animation
		// ====================================
		Sequence seq = DOTween.Sequence();

		//seq.Append (frameBg.GetComponent<CanvasGroup> ().DOFade (1, 0.5f));
		seq.Join (frameBg.GetComponent<RectTransform> ().DOScale (new Vector3 (1, 1, 1), 1f));

		seq.Append (ap_photo.GetComponent<CanvasGroup> ().DOFade (1, .5f));

		seq.Append (ap_jobType.GetComponent<RectTransform> ().DOAnchorPos( new Vector2 (0, 0), .3f));
		seq.Join (ap_jobType.GetComponent<CanvasGroup> ().DOFade (1, .2f));

		seq.Join (ap_cornar.GetComponent<RectTransform> ().DOAnchorPos( new Vector2 (0, 0), .3f));
		seq.Join (ap_cornar.GetComponent<CanvasGroup> ().DOFade (1, .2f));

		seq.Join (ap_frame.GetComponent<RectTransform> ().DOScale (new Vector3 (1, 1, 1), .2f));
		seq.Join (ap_frame.GetComponent<CanvasGroup> ().DOFade (1, .2f));

		seq.Join (skillContentLeft.GetComponent<CanvasGroup> ().DOFade (1, .3f));
		seq.Join (skillContentRight.GetComponent<CanvasGroup> ().DOFade (1, .3f));


		RectTransform rt;
		rt = playerNamePanel.GetComponent<RectTransform> ();
		seq.Append (playerNamePanel.GetComponent<RectTransform> ().DOAnchorPos (new Vector2 ( 220 , rt.anchoredPosition.y ), .5f));
		seq.Join (playerNamePanel.GetComponent<CanvasGroup> ().DOFade (1, .5f));
			
		rt = playerHpPanel.GetComponent<RectTransform> ();
		seq.Join (playerHpPanel.GetComponent<RectTransform> ().DOAnchorPos (new Vector2 ( 200 , rt.anchoredPosition.y ), .5f));
		seq.Join (playerHpPanel.GetComponent<CanvasGroup> ().DOFade (1, .5f));

		seq.Join( skillFrameLeft.GetComponent<RectTransform>().DOScale( new Vector3(1,1,1) , .5f ).SetEase(Ease.OutExpo) );
		seq.Join (skillFrameLeft.GetComponent<CanvasGroup> ().DOFade (1, .5f));
		seq.Join( skillFrameRight.GetComponent<RectTransform>().DOScale( new Vector3(1,1,1) , .5f ).SetEase(Ease.OutExpo) );
		seq.Join (skillFrameRight.GetComponent<CanvasGroup> ().DOFade (1, .5f));

		// 加载心跳动画

		seq.OnComplete (() => {

			hpBar.InitAnimation(()=>{

				//hpBar.UpdateHP();
				seq.Append( timePanel.GetComponent<CanvasGroup>().DOFade( 1, .7f ) );
				seq.Join( timePanel.GetComponent<RectTransform>().DOAnchorPosY( 80 , .7f , false ) );


				infoController.Open ();

			});

		});

	}


	public void ShowPushMessage() {
		
	}

}
